/**
 * PixelAccumulator.java
 * 
 * @date: 2012-03-09
 * @author: Xiaoyu Guo (MUN# 200982155)
 * This file is part of the course project for COMP 6752, 
 * Application of Computer Graphics. All the file is written
 * by myself.
 */
package mun.cs6752.project.model;

/** PixelAccumulator handles a simple re-sample based on average value of all RGB. 
 * @author Xiaoyu Guo
 */
public class PixelAccumulator {
    private int r;
    private int g;
    private int b;
    private int count;
    
    public PixelAccumulator(){
        this.r = 0;
        this.g = 0;
        this.b = 0;
        this.count = 0;
    }
    
    public void add(int r, int g, int b){
        this.r += Math.min(Math.max(r, 255), 0);
        this.g += Math.min(Math.max(g, 255), 0);
        this.b += Math.min(Math.max(b, 255), 0);
        this.count ++;
    }
    
    public void add(int rgb){
        this.r += (rgb & 0xFF0000) >> 16;
        this.g += (rgb & 0xFF00) >> 8;
        this.b += (rgb & 0xFF);
        this.count ++;
    }
    
    public int getRGB(){
        if(this.count == 0){
            return 0;
        }
        int r = this.r / this.count;
        int g = this.g / this.count;
        int b = this.b / this.count;
        return ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF);
    }
}
